<?php

$AppDir = dirname(__FILE__);

/**
 * Include path setup (assuming no facility to edit php.ini)
 */
set_include_path(
    $AppDir . PATH_SEPARATOR
    . $AppDir . '/extends' . PATH_SEPARATOR
    . get_include_path()
);

/**
 * Load configuration
 */
require_once 'Zend/Config/Ini.php';
if (file_exists($AppDir . '/config/config.ini')) {
    $configFile = $AppDir . '/config/config.ini';
} else {
    $configFile = $AppDir . '/config/config.ini.dist';
}
$Config = new Zend_Config_Ini($configFile, 'general');
$ConfigView = $Config->view;

/**
 * Environmental Settings
 */
ini_set('display_errors', $Config->bootstrap->displayerrors);
error_reporting((int) $Config->bootstrap->reporting);
date_default_timezone_set($Config->bootstrap->timezone);

/**
 * Create the View Factory
 */
require_once 'Zps/View/Factory.php';
$ViewFactory = new Zps_View_Factory($ConfigView);

/**
 * Create the Registry
 */
require_once 'Zend/Registry.php';
$Registry = Zend_Registry::getInstance();
$Registry->set('Configuration', $Config);
$Registry->set('BasePath', $AppDir);
$Registry->set('ViewFactory', $ViewFactory);

/**
 * Create the Front Controller
 */
require 'Zend/Controller/Front.php';
$Front = Zend_Controller_Front::getInstance();

/**
 * Setup and Run the Front Controller
 */
$Front->throwExceptions((bool) $Config->bootstrap->renderexceptions);
if (!empty($Config->bootstrap->baseurl)) {
    $Front->setBaseUrl($Config->bootstrap->baseurl);
}
$Front->setControllerDirectory(array(
    'default'=> $AppDir . '/default/controllers', // @todo: move to a config file
    'catalog'=> $AppDir . '/Catalog/controllers',
));
$Front->setParam('Registry', $Registry);
$Front->returnResponse(true);

/**
 * Dispatch the Request and send the Response
 */
$Response = $Front->dispatch();
$Response->sendResponse();